/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.smart.helper;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.smart.common.utils.AssertUtil;
import com.smart.context.SessionContext;
import com.smart.mapper.JiansheProjectMapper;
import com.smart.mapper.ProjectMapper;
import com.smart.mapper.ProjectReportMapper;
import com.smart.mapper.ProjectWeeklyTaskMapper;
import com.smart.model.convert.ProjectConvert;
import com.smart.model.domain.ProjectDO;
import com.smart.model.domain.ProjectWeeklyTaskDO;
import com.smart.model.request.ProjectReportStatisticsRequest;
import com.smart.model.request.ProjectWeeklyTaskRequest;
import com.smart.model.vo.ProjectVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author zhanghong
 * @version WeeklyHelper.java, v 0.1 2023/7/19 19:39 下午 WeeklyHelper Exp $$
 */
@Slf4j
@Component
public class WeeklyHelper {
    @Autowired
    private ProjectWeeklyTaskMapper projectWeeklyTaskMapper;
    @Autowired
    private JiansheProjectMapper jiansheProjectMapper;
    @Autowired
    private ProjectReportMapper projectReportMapper;

    private String getFileName(ProjectWeeklyTaskRequest request) {
        String fileNameTemplate = "[%s年第%s周(%s~%s)][%s]周报项目名单-%s";
        Long year = request.getYear();
        Long weekNum = request.getWeekNum();
        String startDateStr = DateUtil.format(request.getStartTime(), "MM-dd");
        String endDateStr = DateUtil.format(request.getEndTime(), "MM-dd");
        String reportStatus = request.getReportStatus() == 0 ? "未提报" : "已提报";
        String date = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
        String fileName = String.format(fileNameTemplate, year, weekNum, startDateStr, endDateStr, reportStatus, date);
        return fileName;
    }

    /**
     * 查询导出数据
     * @param request
     * @return left-文件名 right-导出数据
     */
    public Pair<String, List<ProjectVO>> queryExportInfo(ProjectWeeklyTaskRequest request) {
        // 前置校验
        AssertUtil.assertNotNull(request.getYear(), "年份不能为空");
        AssertUtil.assertNotNull(request.getWeekNum(), "周数不能为空");
        ProjectWeeklyTaskDO taskDO = projectWeeklyTaskMapper.queryByYearAndWeek(request.getYear().intValue(), request.getWeekNum().intValue());
        AssertUtil.assertNotNull(taskDO, "未查询到数据");
        String fileName = getFileName(request);
        // 建设单位只能看到当前所关联项目的提报数据
        if (SessionContext.getJiansheUnitId() != null) {
            List<Long> relationProjectList = jiansheProjectMapper.selectAllProjectId(SessionContext.getJiansheUnitId());
            if (CollectionUtils.isEmpty(relationProjectList)) {
                log.info("当前建设单位未关联任何项目");
                return Pair.of(fileName, Collections.emptyList());
            }
            request.setProjectIds(relationProjectList);
        }

        //查询提报数据
        ProjectReportStatisticsRequest req = new ProjectReportStatisticsRequest();
        req.setTaskId(taskDO.getId());
        req.setProjects(request.getProjectIds());
        req.setReportStatus(request.getReportStatus());
        req.setStreetCode(request.getStreetCode());
        req.setConstructionStatus(request.getProjectStatus());
        log.info("查询参数req ={}", JSON.toJSONString(req));
        List<ProjectDO> projects = projectReportMapper.listEffectProjectByCondition(req);

        // 内存分页
        List<ProjectVO> projectVOS = ProjectConvert.buildProjectVoList(projects);
        return Pair.of(fileName, projectVOS);
    }
}
